using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace tecmosuperbowlmanager
{
    public class romReader
    {        
        private string _fileName;
        private string FileName
        {
            get { return _fileName; }
            set 
            {
                if (File.Exists(value) == false) { throw new FileNotFoundException("File not Found"); }
                _fileName = value; 
            }
        }

        private List<byte> _bytesOfFile;
        public List<byte> BytesOfFile
        {
            get { return _bytesOfFile; }
            set { _bytesOfFile = value; }
        }

        private int _quarterLength;
        public int QuarterLength
        {
            get { return _quarterLength; }
            set
            {
                //make this throw a QuarterLengthException instead
                //this also has a max of 15. this is because a hex value such as '0a' is messed up. I am unsure how exactly the game handles this value.      
                if (value > 159 || value < 0) { throw new Exception("Quarter length of " + value + " is not acceptbale..."); }
                else { 
                    _quarterLength = value;                    
                    int ones = _quarterLength % 10;
                    int tens = (_quarterLength - ones) / 10;
                    string hexValue = Convert.ToString(tens, 16).PadLeft(1, '0') + Convert.ToString(ones, 16).PadLeft(1, '0');
                    byte quarterByte = byte.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
                    BytesOfFile[QuarterLengthLocation] = quarterByte;
                }
            }
        }

        private int _quarterLengthLocation;
        public int QuarterLengthLocation
        {
            get { return _quarterLengthLocation; }
            set { _quarterLengthLocation = value; }
        }

        /// <summary>
        /// supply the pathname and it will make the BytesOfFile property be populated
        /// </summary>
        /// <param name="PathNameOfFile"></param>
        public romReader(string PathNameOfFile)
        {
            FileName = PathNameOfFile;         
            BytesOfFile = new List<byte>();                        
            readFile();
            findQuarterLength();
        }

        /// <summary>
        /// this will read in a file based on FileName and store the bytes into the list called BytesOfFile
        /// </summary>
        private void readFile()
        {
            FileStream fs = new FileStream(FileName, FileMode.Open);
            BinaryReader binReader = null;
            try
            {
                binReader = new BinaryReader(fs);
             //   binReader = new BinaryReader(File.Open(FileName, FileMode.Open));
                byte b = binReader.ReadByte();
                while (true)
                {
                    // Console.Write(Convert.ToString(b, 16).PadLeft(2, '0') + "\t");
                    BytesOfFile.Add(b);
                    b = binReader.ReadByte();
                }

            }
            catch (FileNotFoundException e)
            {
                Console.WriteLine("File Not Found...\n");
            }
            catch (EndOfStreamException e)
            {
                //Finished Reading File                
            }
            finally
            {
                if (binReader != null)
                {
                    binReader.Close();
                    fs.Close();
                }                
            }
        }

        /*************************************************
         * Quarter Length
         *   0x2224B is location for quarter length
         *   http://www.knobbe.org/phpBB2/viewtopic.php?p=77076#77076
         *       '01' is 1 minute
         *       '09' is 9 minutes
         *       '10' is 10 minutes
         *       'a0' is 100 minutes
         *       'f9' is 249 minutes
         * ***********************************************/
        private void findQuarterLength()
        {
            int binaryLocation = int.Parse("2224B", System.Globalization.NumberStyles.HexNumber);
            QuarterLengthLocation = binaryLocation;
            //Console.WriteLine(binaryLocation);
            Byte quarter_byte = BytesOfFile[QuarterLengthLocation];
            //Console.WriteLine(quarter_byte.ToString());
            string quarter_string = Convert.ToString(quarter_byte, 16).PadLeft(2, '0');

            //'a0', the 'a' is the 16^2 place, the '0' is the 16s place
            string sixteen_squared_place = quarter_string.Substring(0, 1);
            string sixteens_place = quarter_string.Substring(1, 1);

            int tens_place = int.Parse(sixteen_squared_place, System.Globalization.NumberStyles.HexNumber);
            int ones_place = int.Parse(sixteens_place, System.Globalization.NumberStyles.HexNumber);            
            QuarterLength = tens_place*10 + ones_place;
        }
    }
}
